package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.mongo.Option;
import com.tanhua.dubbo.api.mongo.OptionApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service(timeout = 10000)
public class OptionApiImpl implements OptionApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Option> findByQid(ObjectId qqId) {
        Query query = new Query(Criteria.where("questionId").is(qqId));
        return mongoTemplate.find(query, Option.class);
    }

    @Override
    public void save(Option o) {
        mongoTemplate.save(o);
    }

    @Override
    public Option findForScore(String optionId, String questionId) {
        Query query = new Query(Criteria.where("_id").is(new ObjectId(optionId)));
        return mongoTemplate.findOne(query,Option.class);
    }
}
